package henu.graduation_design.adapter;

import henu.graduation_design.onlineshop.ProductAppraiseActivity;
import henu.graduation_design.onlineshop.R;
import henu.graduation_design.util.AsyncImageLoader;
import henu.graduation_design.util.JsonLoader;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class OrderAdapter extends BaseAdapter {

	private Context context;
	private List<HashMap<String, Object>> list;
	private AsyncImageLoader asyncImageLoader = new AsyncImageLoader();
	private JsonLoader jsonLoader = new JsonLoader();
	SharedPreferences sp;

	public OrderAdapter(Context context, List<HashMap<String, Object>> list,SharedPreferences sp) {
		this.context = context;
		this.sp=sp;
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ProductHolder holder;
		convertView = LayoutInflater.from(context).inflate(
				R.layout.listview_order, null);
		holder = new ProductHolder();
		holder.imageView = (ImageView) convertView
				.findViewById(R.id.order_image);
		holder.nameTextView = (TextView) convertView
				.findViewById(R.id.order_name);
		holder.priceTextView = (TextView) convertView
				.findViewById(R.id.order_price);
		holder.btnTextView = (TextView) convertView
				.findViewById(R.id.order_btn);

		final HashMap<String, Object> currentItem = list.get(position);

		holder.nameTextView.setText(currentItem.get("name").toString());
		holder.priceTextView.setText("￥"+currentItem.get("price").toString());
		loadImage(currentItem.get("imageUri").toString(), holder.imageView);
		

		
		if(currentItem.get("status").toString().equals("1")){
			holder.btnTextView.setText("确认收货>>");
			holder.btnTextView.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					Map<String,Object> mapEntry=new HashMap<String, Object>();
				    mapEntry.put("productID", currentItem.get("id"));
				    mapEntry.put("orderID", currentItem.get("orderID"));
					jsonLoader.postJson(context.getString(R.string.url_get_send_product),
							sp, mapEntry,new JsonLoader.JsonObjectCallback(){
								public void jsonLoaded(JSONObject jsonObject) {	
								}
					});
					holder.btnTextView.setText("");
					Toast.makeText(context, "已确认收货", Toast.LENGTH_LONG).show();
				}
			});
		}else if(currentItem.get("status").toString().equals("2")){
			holder.btnTextView.setText("评价>>");
			holder.btnTextView.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					Intent intent=new Intent(context,ProductAppraiseActivity.class);
					intent.putExtra("prodcutID", Integer.parseInt(currentItem.get("id").toString()));
					intent.putExtra("orderID", Integer.parseInt(currentItem.get("orderID").toString()));
					holder.btnTextView.setText("");
					context.startActivity(intent);
				}
			});
			
		}else{
			holder.btnTextView.setText("");
		}
		return convertView;
	}

	private void loadImage(final String url, final ImageView imageView) {
		// 如果缓存过就会从缓存中取出图像，ImageCallback接口中方法也不会被执行
		Drawable cacheImage = asyncImageLoader.loadDrawable(url,
				new AsyncImageLoader.ImageCallback() {
					// 请参见实现：如果第一次�釉�rl时下面方法会执行
					public void imageLoaded(Drawable imageDrawable) {
						imageView.setImageDrawable(imageDrawable);
					}
				});
		if (cacheImage != null) {
			imageView.setImageDrawable(cacheImage);
		}
	}

	private class ProductHolder {
		private ImageView imageView;
		private TextView nameTextView;
		private TextView priceTextView;
		private TextView btnTextView;
	}
	
}
